/**
 * Created by 都大爽 on 2017/11/18.
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import { routers } from '../router/router'
// import iView from 'iview'
import store from '../store'
import Util from '../libs/util'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: routers
})
/**
 *
 */
router.beforeEach((to, from, next) => {
  // iView.LoadingBar.start()

if (to.fullPath === '/') {
  router.replace({
    name: 'home_index'
  })
}

if (to.matched.some(record => record.meta && record.meta.requireAuth)) {
  if (!Util.user()){
    Util.setAuthRedirect(to.fullPath)
    next({
      name: 'login'
    })
  } else {
    if (to.meta.requirePermission) {
      if (Util.hasPermission(to.meta.requirePermission)) {
        next()
      } else {
        // next(new Error('403'))
        console.log(Util.user().type)
        switch (Util.user().type)
        {
          case '录入':
            next({
              name: 'only-enroll-situation'
            })
            break;
          case '编辑':
            next({
              path: '/home/all-list/' + Util.user().school_id
            })
            break;
          default:
            next({
              name: 'all'
            })
        }
      }
    } else {
      next()
    }
  }
} else {
  next()
}

// iView.LoadingBar.finish()
})

/**
 *
 */
router.afterEach((to, from) => {
  store.commit('setTitle', to.meta.title)
if (to.name !== 'login') {
  // store.commit('openNewPage', to)
  // store.commit('setBreadCrumbsByRoute', to)
}
// iView.LoadingBar.finish()
window.scrollTo(0, 0)
})

/**
 *
 */
router.onError((error) => {
//   switch (error.message) {
// case '404':
//   router.push({name: 'error_404'})
//   break
// case '403':
//   router.push({name: 'error_403'})
//   break
// }
console.log(error)
})

export default router
